"""
由于直接在服务层调用仓储层对象会出现类型验证错误情况，需要先创建仓储层依赖，并通过依赖进行导入。
"""

from typing import Annotated

from fastapi import Depends
from db.mysql_db import SessionDep
from repositories.permission_repositories import PermissionRepository
from repositories.product_repositories import ProductRepository
from repositories.role_repositories import RoleRepository
from repositories.user_repositories import UserRepository
from services.permission_service import PermissionService
from services.product_service import ProductService
from services.role_service import RoleService
from services.user_service import UserService


"""
将数据库会话依赖注入仓储层，使仓储层可使用数据库会话连接。
SessionDep为数据库连接配置中写定的数据库依赖。
"""
def get_user_repository(db: SessionDep) -> UserRepository:
    return UserRepository(db)

def get_role_repository(db: SessionDep) -> RoleRepository:
    return RoleRepository(db)

def get_permission_repository(db: SessionDep) -> PermissionRepository:
    return PermissionRepository(db)

def get_product_repository(db: SessionDep) -> ProductRepository:
    return ProductRepository(db)

"""给服务层创建依赖"""
def get_user_service(user_repo: UserRepository = Depends(get_user_repository), role_repo: RoleRepository = Depends(get_role_repository)) -> UserService:
    return UserService(user_repo, role_repo)

def get_role_service(role_repo: RoleRepository = Depends(get_role_repository), permission_repo: PermissionRepository = Depends(get_permission_repository)) -> RoleService:
    return RoleService(role_repo, permission_repo)

def get_permission_service(permission_repo: PermissionRepository = Depends(get_permission_repository)):
    return PermissionService(permission_repo)

def get_product_service(product_repo: ProductRepository = Depends(get_product_repository)):
    return ProductService(product_repo)

"""创建服务层依赖并注入控制层"""
UserServiceDep = Annotated[UserService, Depends(get_user_service)] # 用户服务依赖

RoleServiceDep = Annotated[RoleService, Depends(get_role_service)] # 角色服务依赖

PermissionServiceDep= Annotated[PermissionService, Depends(get_permission_service)] # 权限服务依赖

ProductServiceDep = Annotated[ProductService, Depends(get_product_service)] # 产品模块依赖